/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.manager.impl;

import com.xy.biz.ts.dao.*;
import com.xy.biz.ts.domain.bo.ExaminationPaperBO;
import com.xy.biz.ts.domain.bo.ExaminationPaperQuestionBO;
import com.xy.biz.ts.domain.bo.ExaminationPaperRuleBO;
import com.xy.biz.ts.domain.bo.ExaminationQuestionBO;
import com.xy.biz.ts.domain.converter.ExaminationPaperConverter;
import com.xy.biz.ts.domain.converter.ExaminationPaperQuestionConverter;
import com.xy.biz.ts.domain.converter.ExaminationPaperRuleConverter;
import com.xy.biz.ts.domain.converter.ExaminationQuestionConverter;
import com.xy.biz.ts.domain.po.ExaminationPaperPO;
import com.xy.biz.ts.domain.po.ExaminationPaperQuestionPO;
import com.xy.biz.ts.domain.po.ExaminationPaperRulePO;
import com.xy.biz.ts.domain.po.ExaminationQuestionPO;
import com.xy.biz.ts.domain.query.ExaminationPaperQuery;
import com.xy.biz.ts.domain.req.GetExaminationPaperStatusRequest;
import com.xy.biz.ts.domain.resp.GetExaminationPaperResponse;
import com.xy.biz.ts.manager.ExaminationPaperManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 *
 *
 * @author wcs
 * @date 2021-3-8
 */
@Service
public class ExaminationPaperManagerImpl implements ExaminationPaperManager {

	@Autowired
	private ExaminationPaperDao examinationPaperDao;

	@Autowired
	private ExaminationPaperQuestionDao examinationPaperQuestionDao;

	@Autowired
	private ExaminationPaperRuleDao examinationPaperRuleDao;

	@Autowired
	private ExaminationQuestionDao examinationQuestionDao;

	@Autowired
	private ExaminationQuestionOptionDao examinationQuestionOptionDao;

	@Override
	public int saveExaminationPaper(ExaminationPaperBO examinationPaperBO) {
		return examinationPaperDao.saveExaminationPaper(ExaminationPaperConverter.toPO(examinationPaperBO));
	}


	@Override
	public int saveAllExaminationPaper(Collection<ExaminationPaperBO> collection) {
		List<ExaminationPaperPO> examinationPaperPOS;
		return examinationPaperDao.saveAllExaminationPaper(ExaminationPaperConverter.toPO(collection));
	}

	//编辑试卷
	@Transactional
	@Override
	public int updateExaminationPaper(ExaminationPaperBO examinationPaperBO) {
		ExaminationPaperPO examinationPaperPO = ExaminationPaperConverter.toPO(examinationPaperBO);
		return examinationPaperDao.updateExaminationPaper(examinationPaperPO);
	}


	@Override
	public int saveExaminationPaperQuestion(Collection<ExaminationPaperQuestionBO> examinationPaperBO) {
		return examinationPaperQuestionDao.saveAllExaminationPaperQuestion(ExaminationPaperQuestionConverter.toPO(examinationPaperBO));
	}

	@Override
	public int saveExaminationPaperQuestion(ExaminationPaperQuestionBO examinationPaperBO) {
		return examinationPaperQuestionDao.saveExaminationPaperQuestion(ExaminationPaperQuestionConverter.toPO(examinationPaperBO));
	}


	//添加随机试题规则
	@Override
	public int saveExaminationPaperRule(Collection<ExaminationPaperRuleBO> examinationPaperRuleBO) {
		return examinationPaperRuleDao.saveAllExaminationPaperRule(ExaminationPaperRuleConverter.toPO(examinationPaperRuleBO));
	}

	//查找固定类型
	@Override
	public GetExaminationPaperResponse getFixedExaminationPaper(Long examinationPaperId) {
		List<Long> list = new ArrayList<>();
		ExaminationPaperPO examinationPaper = examinationPaperDao.getExaminationPaper(examinationPaperId);
		ExaminationPaperBO examinationPaperBO = ExaminationPaperConverter.fromPO(examinationPaper);
		GetExaminationPaperResponse paperResponse = ExaminationPaperConverter.toResponse(examinationPaperBO);
		List<ExaminationPaperQuestionPO> questionPOList = examinationPaperQuestionDao.getExaminationPaperQuestion(examinationPaperId);
		questionPOList.stream().forEach(e -> list.add(e.getExaminationQuestionId()));
		if (list.size() > 0) {
			List<ExaminationQuestionPO> examinationQuestionPOList = examinationQuestionDao.listExaminationQuestionById(list);
			List<ExaminationQuestionBO> questionBOList = ExaminationQuestionConverter.fromPO(examinationQuestionPOList);
			paperResponse.setQuestionBOList(questionBOList);
		}

//		questionBOList.stream().forEach(e -> e.setOptionList(ExaminationQuestionOptionConverter.fromPO(examinationQuestionOptionDao.getExaminationQuestionOption(e.getExaminationQuestionId()))));
		return paperResponse;
	}

	@Override
	public GetExaminationPaperResponse getRandomExaminationPaper(Long examinationPaperId) {
		ExaminationPaperPO examinationPaper = examinationPaperDao.getExaminationPaper(examinationPaperId);
		ExaminationPaperBO examinationPaperBO = ExaminationPaperConverter.fromPO(examinationPaper);
		GetExaminationPaperResponse paperResponse = ExaminationPaperConverter.toResponse(examinationPaperBO);

		List<ExaminationPaperRulePO> rulePOList = examinationPaperRuleDao.listExaminationPaperRuleById(Arrays.asList(examinationPaperId));
		List<ExaminationPaperRuleBO> paperRuleBOList = ExaminationPaperRuleConverter.fromPO(rulePOList);

		paperResponse.setPaperQuestionBOList(paperRuleBOList);
		return paperResponse;
	}

	/**
	 * 修改状态
	 * @param request
	 * @return
	 */
	@Transactional
	@Override
	public int updateStatusExaminationPaper(GetExaminationPaperStatusRequest request) {
		return examinationPaperDao.updateStatusExaminationPaper(request);
	}

	@Override
	public int deleteRuleExaminationPaper(Long examinationPaperRuleId) {
		return examinationPaperRuleDao.deleteRuleExaminationPaper(examinationPaperRuleId);
	}

	@Override
	public int deleteQuestionExaminationPaper(Long examinationPaperQuestionId) {
		return examinationPaperQuestionDao.deleteQuestionExaminationPaper(examinationPaperQuestionId);
	}

	@Override
	public Integer getNextSequence() {
		return examinationPaperQuestionDao.getNextSequence();
	}

    @Override
    public int deleteExaminationPaperMultiple(Collection<Long> collection) {
        return examinationPaperDao.deleteExaminationPaperMultiple(collection);
    }

    @Override
	public int deleteExaminationPaper(Long examinationPaperId) {
		return examinationPaperDao.deleteExaminationPaper(examinationPaperId);
	}

	@Override
	public ExaminationPaperBO getExaminationPaper(Long examinationPaperId) {
		return ExaminationPaperConverter.fromPO(examinationPaperDao.getExaminationPaper(examinationPaperId));
	}

	@Override
	public List<ExaminationPaperBO> listExaminationPaperById(Collection<Long> collection) {
		if(CollectionUtils.isEmpty(collection)) {
			return Collections.emptyList();
		}
		return ExaminationPaperConverter.fromPO(examinationPaperDao.listExaminationPaperById(collection));
	}

	@Override
	public List<ExaminationPaperBO> listExaminationPaper(ExaminationPaperQuery query) {
		return ExaminationPaperConverter.fromPO(examinationPaperDao.listExaminationPaper(query));
	}

	@Override
	public int countExaminationPaper(ExaminationPaperQuery query) {
		return examinationPaperDao.countExaminationPaper(query);
	}

}
